package com.github.ginirohikocha.dbm.dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.github.ginirohikocha.dbm.R;
import com.github.ginirohikocha.dbm.activity.OffersActivity;
import com.github.ginirohikocha.dbm.entity.Offer;

import java.math.BigDecimal;
import java.util.List;

public class NewOfferDialog extends Dialog {

    private String orderNum;
    private List<Offer> offers;

    private OffersActivity offersActivity;
    private TextView title, description, price,
                confirm, cancel;

    public NewOfferDialog(@NonNull Context context, String orderNum, List<Offer> offers) {
        super(context);
        this.offersActivity = (OffersActivity)context;
        this.orderNum = orderNum;
        this.offers = offers;
        init();
    }

    private void init() {
        setContentView(R.layout.dialog_new_offer);
        title = findViewById(R.id.dialog_new_offer_title);
        description = findViewById(R.id.dialog_new_offer_description);
        price = findViewById(R.id.dialog_new_offer_price);
        confirm = findViewById(R.id.dialog_new_offer_confirm);
        cancel = findViewById(R.id.dialog_new_offer_cancel);

        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Offer offer = new Offer(null,
                        orderNum,
                        1,
                        title.getText().toString(),
                        description.getText().toString(),
                        null,
                        BigDecimal.valueOf(Double.valueOf(price.getText().toString())),
                        "未完成");
                offers.add(offer);
                offersActivity.refresh();
                cancel();
            }
        });
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cancel();
            }
        });
    }
}
